<?php

class Team {

    public $id;
    public $full_name;
    public $short_name;
    public $abbr;

    function __construct($full_name = '', $short_name = '', $abbr = '') {
        $this->full_name = $full_name;
        $this->short_name = $short_name;
        $this->abbr = $abbr;
    }

    /**
     * Load team from db by ID;
     * @param type $id
     * @return Team
     */
    public static function fromID($id) {
        $instance = new self();
        $instance->loadByID($id);
        return $instance;
    }

     /**
     * Load team from database row;
     * @param array $row
     * @return Team
     */
    public static function fromRow(array $row) {
        $instance = new self();
        $instance->fill($row);
        return $instance;
    }

    private function loadByID($id) {
        $result = mysql_query("SELECT `id`,`full_name`,`short_name`,`abbr` FROM `teams` WHERE `id`='$id'");
        if (mysql_num_rows($result) == 0) {
            return null;
        }
        $row = mysql_fetch_assoc($result);
        $this->fill($row);
    }

    private function fill(array $row) {
        $this->id = $row['id'];
        $this->full_name = $row['full_name'];
        $this->short_name = $row['short_name'];
        $this->abbr = $row['abbr'];
    }

    public function toArray() {
        return get_object_vars($this);
    }

    public function getID() {
        return $this->id;
    }

    public function getFullName() {
        return $this->full_name;
    }

    public function setFullName($full_name) {
        $this->full_name = $full_name;
        return $this;
    }

    public function getShortName() {
        return $this->short_name;
    }

    public function setShortName($short_name) {
        $this->short_name = $short_name;
        return $this;
    }

    public function getAbbr() {
        return $this->abbr;
    }

    public function setAbbr($abbr) {
        $this->abbr = $abbr;
        return $this;
    }

    public function __toString() {
        return "full_name: $this->full_name, short_name: $this->short_name, abbr: $this->abbr<br>";
    }

    public function toTableRow() {
        return "<tr><td>$this->full_name</td><td>$this->short_name</td><td>$this->abbr</td></tr>";
    }

    public function toTableColumns() {
        return "<td>$this->full_name</td><td>$this->short_name</td><td>$this->abbr</td>";
    }

}

?>
